using System;
using System.Xml;

namespace FreeFlow.Client
{
  /// <summary>Operation that occurs when the button is pressed</summary>
  public enum ButtonOperation
  {
    /// <summary>The button submits the current action.</summary>
    Submit,
    /// <summary>The button cancels the current action.</summary>
    Cancel,
    /// <summary>The button performs a client operation.</summary>
    Client,
    /// <summary>The button performs a server operation.</summary>
    Server,
    /// <summary>The button opens a folder.</summary>
    Folder,
    /// <summary>The button starts a blank form</summary>
    BlankForm
  }

	/// <summary>
	/// A button field.
	/// </summary>
	[Serializable]
	public class ButtonField : Field
	{
    internal ButtonField(Form form, XmlElement fieldNode)
      : base(form, fieldNode)
		{
      if (fieldNode != null)
      {
        caption = fieldNode.GetAttribute("Caption");
        string operationStr = fieldNode.GetAttribute("Operation");
        if (operationStr == "Commit")
          operation = ButtonOperation.Submit;
        else if (operationStr == "Cancel")
          operation = ButtonOperation.Cancel;
        else if (operationStr == "Server")
          operation = ButtonOperation.Server;
        else if (operationStr == "Folder")
          operation = ButtonOperation.Folder;
        else if (operationStr == "Action")
          operation = ButtonOperation.BlankForm;
        else if (operationStr == "Client")
          operation = ButtonOperation.Client;
        else
          throw new FreeFlowException("Unrecognized button action - " + operationStr);
        blankFormProcess = fieldNode.GetAttribute("Map");
        blankFormAction = fieldNode.GetAttribute("Action");
      }
		}

    #region properties
    private string caption;
    /// <summary>
    /// Gets the caption of the button.
    /// </summary>
    public string Caption
    {
      get { return caption; }
    }

    private ButtonOperation operation;
    /// <summary>
    /// Gets the operation that occurs when the button is pressed.
    /// </summary>
    public ButtonOperation Operation
    {
      get { return operation; }
    }

    private string blankFormProcess;
    /// <summary>
    /// For a button whose Operation is BlankForm, gets the blank form process name.
    /// </summary>
    public string BlankFormProcess
    {
      get { return blankFormProcess; }
    }

    private string blankFormAction;
    /// <summary>
    /// For a button whose Operation is BlankForm, gets the blank form action name.
    /// </summary>
    public string BlankFormAction
    {
      get { return blankFormAction; }
    }

    /// <summary>
    /// For a button whose Operation is Folder, gets the folder id to open.
    /// </summary>
    public string OpenFolderId
    {
      get
      {
        if (Value.StartsWith("FOLDER="))
          return Value.Substring(7);
        else
          return string.Empty;
      }
    }
    #endregion

    internal override bool ShouldAddToFieldInput()
    {
      return false;
    }
	}
}
